package com.wangcp.utils;

import cn.hutool.core.date.DateUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


public class DateUtils {

    public static void main(String[] args) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String startStr = "2021-03-12";
        String endStr = "2021-11-23";
        Date startDate = sdf.parse(startStr);
        Date endDate = sdf.parse(endStr);
        System.out.println("--------按季度进行拆分--------");
        List<RangeDate> ranges = splitToQuarts(startDate, endDate);
        for (RangeDate range : ranges) {
            System.out.println("开始时间：" + sdf.format(range.start) +"---------结束时间：" + sdf.format(range.end));
        }

        System.out.println("--------按半年进行拆分--------");
        List<RangeDate> ranges1 = splitToHalfOfYears(startDate, endDate);
        for (RangeDate range : ranges1) {
            System.out.println("开始时间：" + sdf.format(range.start) +"---------结束时间：" + sdf.format(range.end));
        }
    }

    //将时间段按星期分割
    public static final List<RangeDate> splitToWeeks(Date start, Date end) {
        List<RangeDate> result = new ArrayList<>();
        result.add(RangeDate.create(start));
        Date from = new Date(start.getTime() + 7L * 24 * 3600 * 1000);
        Date weekEnd = cn.hutool.core.date.DateUtil.endOfWeek(end);
        while (from.compareTo(weekEnd) <= 0) {
            Date dt = cn.hutool.core.date.DateUtil.beginOfWeek(from);

            CollectionTools.last(result).end(new Date(dt.getTime() - 24L * 3600 * 1000));
            result.add(RangeDate.create(dt));
            from.setTime(from.getTime() + 7L * 24 * 3600 * 1000);
        }
        CollectionTools.last(result).end(end);
        return result;
    }

    //将时间段按照月分割
    public static final List<RangeDate> splitToMonths(Date start, Date end) {
        List<RangeDate> result = new ArrayList<>();
        result.add(RangeDate.create(start));
        Calendar cal = Calendar.getInstance();
        cal.setTime(start);
        cal.add(Calendar.MONTH, 1);
        Date monthEnd = cn.hutool.core.date.DateUtil.endOfMonth(end);
        while (cal.getTimeInMillis() <= monthEnd.getTime()) {
            Date dt = cn.hutool.core.date.DateUtil.beginOfMonth(cal.getTime());
            CollectionTools.last(result).end(new Date(dt.getTime() - 24L * 3600 * 1000));
            result.add(RangeDate.create(dt));
            cal.add(Calendar.MONTH, 1);
        }
        CollectionTools.last(result).end(end);
        return result;
    }

    //将时间段按照季度分割
    public static final List<RangeDate> splitToQuarts(Date start, Date end) {
        List<RangeDate> result = new ArrayList<>();
        result.add(RangeDate.create(start));
        Calendar cal = Calendar.getInstance();
        cal.setTime(start);
        cal.add(Calendar.MONTH, 3);
        Date quartEnd = cn.hutool.core.date.DateUtil.endOfQuarter(end);
        while (cal.getTimeInMillis() <= quartEnd.getTime()) {
            Date dt = cn.hutool.core.date.DateUtil.beginOfQuarter(cal.getTime());
            CollectionTools.last(result).end(new Date(dt.getTime() - 24L * 3600 * 1000));
            result.add(RangeDate.create(dt));
            cal.add(Calendar.MONTH, 3);
        }
        CollectionTools.last(result).end(end);
        return result;
    }

    //将时间段按照半年分割
    public static final List<RangeDate> splitToHalfOfYears(Date start, Date end) {
        List<RangeDate> result = new ArrayList<>();
        result.add(RangeDate.create(start));
        Calendar cal = Calendar.getInstance();
        cal.setTime(start);
        cal.add(Calendar.MONTH, 6);
        int mon = cal.get(Calendar.MONTH);
        Calendar tmp = Calendar.getInstance();
        if (mon < 6) {
            tmp.setTimeInMillis(cal.getTimeInMillis());
            tmp.set(Calendar.MONTH, 5);
        } else {
            tmp.setTimeInMillis(cal.getTimeInMillis());
            tmp.set(Calendar.MONTH, 11);
        }
        Calendar halfYearEnd = cn.hutool.core.date.DateUtil.endOfMonth(tmp);
        while (cal.getTimeInMillis() <= halfYearEnd.getTimeInMillis()) {
            mon = cal.get(Calendar.MONTH);
            Calendar cal1 = Calendar.getInstance();
            if (mon < 6) {
                cal1.setTimeInMillis(cal.getTimeInMillis());
                cal1.set(Calendar.MONTH, 0);
            } else {
                cal1.setTimeInMillis(cal.getTimeInMillis());
                cal1.set(Calendar.MONTH, 6);
            }
            Date dt = cn.hutool.core.date.DateUtil.beginOfMonth(cal1.getTime());
            CollectionTools.last(result).end(new Date(dt.getTime() - 24L * 3600 * 1000));
            result.add(RangeDate.create(dt));
            cal.add(Calendar.MONTH, 6);
        }
        CollectionTools.last(result).end(end);
        return result;
    }

    //将时间段按照年分割
    public static final List<RangeDate> splitToYears(Date start, Date end) {
        List<RangeDate> result = new ArrayList<>();
        result.add(RangeDate.create(start));
        Calendar cal = Calendar.getInstance();
        cal.setTime(start);
        cal.add(Calendar.YEAR, 1);
        Date yearEnd = cn.hutool.core.date.DateUtil.endOfYear(end);
        while (cal.getTimeInMillis() <= yearEnd.getTime()) {
            Date dt = cn.hutool.core.date.DateUtil.beginOfYear(cal.getTime());
            CollectionTools.last(result).end(new Date(dt.getTime() - 24L * 3600 * 1000));
            result.add(RangeDate.create(dt));
            cal.add(Calendar.YEAR, 1);
        }
        CollectionTools.last(result).end(end);
        return result;
    }



}
